<?php

namespace app\admin\controller\crud;

use app\admin\controller\AdminController;
use app\admin\service\crud\CodeService;
use app\admin\validate\crud\CodeValidate;
use think\response\Json;

class Code extends AdminController
{
    /**
     * 查看代码生成列表
     * @return Json
     */
    public function lists()
    {
        return $this->data(CodeService::getList());
    }

    /**
     * 导入数据表
     * @return Json
     */
    public function import() {
        $params = (new CodeValidate())->post()->checkParam('import');
        $result =  CodeService::import($params['tables'], $this->adminId);
        if($result) {
            return $this->success([], '导入成功');
        } else {
            return $this->error(CodeService::getError());
        }
    }

    /**
     * 代码生成详情
     * @return Json
     */
    public function detail()
    {
        $params = (new CodeValidate())->checkParam('detail');
        $result = CodeService::detail($params);
        return $this->data($result);
    }

    /**
     * 代码预览
     * @return Json
     */
    public function preview()
    {
        $params = (new CodeValidate())->post()->checkParam('preview');
        $result = CodeService::preview($params);
        return $this->data($result);
    }

    /**
     * 生成代码
     * @return Json
     */
    public function generate()
    {
        $params = (new CodeValidate())->post()->checkParam('generate');
        $result = CodeService::generate($params);
        if($result) {
            return $this->success($result, '生成成功');
        } else {
            return $this->error(CodeService::getError());
        }
    }

    /**
     * 同步表结构
     * @return Json
     */
    public function sync()
    {
        $params = (new CodeValidate())->post()->checkParam('sync');
        $result = CodeService::sync($params, $this->adminId);
        if($result) {
            return $this->success($result, '同步成功');
        } else {
            return $this->error(CodeService::getError());
        }
    }

    /**
     * 编辑代码生成
     * @return Json
     */
    public function edit()
    {
        $params = (new CodeValidate())->post()->checkParam('edit');
        $result = CodeService::edit($params);
        if(true === $result) {
            return $this->success([], '编辑成功');
        } else {
            return $this->error(CodeService::getError());
        }
    }

    /**
     * 删除代码生成
     * @return Json
     */
    public function delete()
    {
        $params = (new CodeValidate())->delete()->checkParam('delete');
        $result = CodeService::delete($params);
        if(true === $result) {
            return $this->success([], '删除成功');
        } else {
            return $this->error(CodeService::getError());
        }
    }

}